package com.zigong.app.ui.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zigong.app.R;
import com.zigong.app.beans.FChannel;
import com.zigong.app.beans.FocusColumn;
import com.zigong.app.beans.TvItem;
import com.zigong.app.ui.fragment.AnchorFragment;
import com.zigong.app.ui.fragment.CityStateFragment;
import com.zigong.app.ui.fragment.ColumnBaseFragment;
import com.zigong.app.ui.fragment.CommonFragment;
import com.zigong.app.ui.fragment.GreatestFragment;
import com.zigong.app.ui.fragment.LiveFragment;
import com.zigong.app.ui.fragment.NewsListFragment;
import com.zigong.app.ui.fragment.ProgramBackFragment;
import com.zigong.app.ui.fragment.RadioFragment;
import com.zigong.app.ui.fragment.SpecialFragment;
import com.zigong.app.ui.fragment.TvChannelFragment;
import com.zigong.app.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: create by Administrator on 2018/6/25 15 29
 * Des:
 */
public class TVPagerAdapter extends FragmentPagerAdapter {

    private Context context;
    public static final int TYPE_TV = 1;//电视台
    public static final int TYPE_VIDEO = 2;//电台
    public static final int TYPE_BACK = 3;//新闻回看

    private List<TvItem> dataList = new ArrayList<>();
    private TvFragment parentFragment;

    public TVPagerAdapter(Context context, TvFragment parentFragment, FragmentManager fm, List<TvItem> list) {
        super(fm);
        if(null!=list&&list.size()>0) {
            dataList.clear();
            this.dataList.addAll(list);
        }
        this.parentFragment = parentFragment;
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (dataList.get(position).getType()) {
            case TYPE_TV:
                fragment = new TvChannelFragment();
                ((TvChannelFragment) fragment).setData(dataList.get(position).getChannel());
                ((TvChannelFragment) fragment).setParentFragment(parentFragment);
                break;
            case TYPE_VIDEO:
                fragment = new RadioFragment();
                ((RadioFragment) fragment).setData(dataList.get(position).getChannel());
                ((RadioFragment) fragment).setParentFragment(parentFragment);
                break;
            case TYPE_BACK:
                fragment = new ProgramBackFragment();
                ((ProgramBackFragment)fragment).setQuestUrl(dataList.get(position).getUrl());
                ((ProgramBackFragment) fragment).setParentFragment(parentFragment);
                break;
            default:
                fragment = new TvChannelFragment();
                ((TvChannelFragment) fragment).setData(dataList.get(position).getChannel());
                ((TvChannelFragment) fragment).setParentFragment(parentFragment);
                break;
        }
        fragment.setIndexInViewPager(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

//    @Override
//    public CharSequence getPageTitle(int position) {
//        return dataList.get(position).getTitle();
//    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
        ImageView iv = (ImageView) view.findViewById(R.id.tab_img);
        TextView tv = (TextView) view.findViewById(R.id.tab_text);
        tv.setText(dataList.get(position).getTitle());
        iv.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getNormalImg()));
        return view;
    }

    public void setData(List<TvItem> data) {
        if((dataList==null||dataList.size()==0)&&(null!=data&&data.size()>0)) {
            dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //如果注释这行，那么不管怎么切换，page都不会被销毁
//        super.destroyItem(container, position, object);
    }


}

